//
//  CharacterStepView.swift
//  SkylowAPP
//
//  Created by 青色石头 on 2024/9/21.
//

import UIKit
import SnapKit

class CharacterStepDotView: UIView {
    
    private lazy var bgImg = {
        let img = UIImageView(image: UIImage(named: "character_step_dot"))
        self.addSubview(img)
        img.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return img
    }()
    
    enum Step {
    case OK
    case current
    case next
    }
    
    var currentStep: Step {
        didSet {
            switch currentStep {
            case .OK:
                bgImg.isHidden = true
                backgroundColor = .colorWith(hexString: "#525252")
            case .current:
                bgImg.isHidden = false
                backgroundColor = .colorWith(hexString: "#525252")
            case .next:
                bgImg.isHidden = true
                backgroundColor = .colorWith(hexString: "#D6D6D6")
            }
        }
    }
    
    override init(frame: CGRect) {
        currentStep = .next
        super.init(frame: frame)
        _ = bgImg
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class CharacterStepView: UIView {
    
    private lazy var contentView = {
        let vw = UIView()
        vw.backgroundColor = .clear
        self.addSubview(vw)
        vw.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(333 / 430.0)
            make.height.equalTo(24)
        }
        return vw
    }()
    
    private lazy var line: CAShapeLayer = {
        let shape = CAShapeLayer()
        shape.backgroundColor = UIColor.colorWith(hexString: "#D6D6D6").cgColor
        shape.strokeColor = UIColor.colorWith(hexString: "#525252").cgColor
        shape.lineWidth = 4
        shape.strokeStart = 0
        shape.strokeEnd = 0
        self.contentView.layer.addSublayer(shape)
        return shape
    }()
    
    private lazy var dots: [CharacterStepDotView] = {
        var dots = [CharacterStepDotView]()
        for i in 0..<4 {
            let dot = CharacterStepDotView()
            dot.clipsToBounds = true
            dot.layer.cornerRadius = 9
            dot.currentStep = .next
            contentView.addSubview(dot)
            dots.append(dot)
        }
        return dots
    }()
    
    // 当前的步骤，从0开始，3截止
    var currentStep: Int = 0 {
        didSet {
            for (index, dot) in dots.enumerated() {
                if index < currentStep {
                    dot.currentStep = .OK
                    dot.transform = .identity
                }else if index == currentStep {
                    dot.currentStep = .current
                    CATransaction.begin()
                    CATransaction.setDisableActions(true)
                    line.strokeEnd = Double(index) / Double(dots.count - 1)
                    CATransaction.commit()
                    dot.transform = CGAffineTransformMakeScale(4 / 3.0, 4 / 3.0)
                }else {
                    dot.currentStep = .next
                    dot.transform = .identity
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(51, 51, 51)
        
        _ = contentView
        _ = line
        _ = dots
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let dotWidth = 18.0
        let lineWidth = contentView.width - dotWidth
        
        let lineHeight = 4.0
        
        line.frame = CGRectMake(dotWidth / 2 , (contentView.height - lineHeight) / 2, lineWidth, lineHeight)
        if line.path == nil {
            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y: lineHeight / 2))
            path.addLine(to: CGPoint(x: lineWidth, y: lineHeight / 2))
            line.path = path.cgPath
        }
        
        for (index, dot) in dots.enumerated() {
            dot.bounds = CGRect(x: 0, y: 0, width: dotWidth, height: dotWidth)
            let xRate = Double(index) / Double(dots.count - 1)
            let x = dotWidth / 2 + xRate * lineWidth
            dot.center = CGPoint(x: x, y: contentView.height / 2)
        }
    }
    
}
